package com.probiz.estore.product.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.product.WholesalePrice;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductSkuDao;
import com.probiz.estore.product.dao.WholesalePriceDao;
import com.probiz.estore.product.service.WholesalePriceManager;


/**
 * Manager implementation for WholesalePrice, responsible for business processing, and communicate between web and persistence layer.
 */
public class WholesalePriceManagerImpl extends GenericManagerImpl<WholesalePrice> implements WholesalePriceManager {

	private WholesalePriceDao wholesalePriceDao = null;
	private ProductSkuDao  productSkuDao = null;

	/**
	 * @param wholesalePriceDao
	 *            the wholesalePriceDao to set
	 */
	public void setWholesalePriceDao(WholesalePriceDao wholesalePriceDao) {
		this.wholesalePriceDao = wholesalePriceDao;
	}

	public void setProductSkuDao(ProductSkuDao productSkuDao) {
		this.productSkuDao = productSkuDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = wholesalePriceDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(WholesalePrice entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(WholesalePrice entity) {

	}

	
	public List<String> saveWholesalePriceFromArrays(String[] skuId, String[] wpSkuIds, String[] wpIds,
			String[] wpMinQuantitys, String[] wpMaxQuantitys,
			String[] wpPrices, String[] wpDeliveryDays){
		List<String> newWpIdList = new ArrayList<String>();
		//删除批发价
		List<String> wpIdList=Arrays.asList(wpIds);
		Set<WholesalePrice> formerWpList=new HashSet<WholesalePrice>();//原来的wholesalePrices
		Set<String> skuIdSet = new HashSet<String>();
		//先去除重复元素
		for(int i=0; i< skuId.length; i++){
			skuIdSet.add(skuId[i]);
		}
		//注意：这里比较特殊，因为一个sku至少有一个wp，所以可以这样找出所有wp
		for(String tempSkuId: skuIdSet){
			formerWpList.addAll(productSkuDao.getById(new Integer(tempSkuId)).getWholesalePrices());
		}
		for (WholesalePrice wp : formerWpList) {
			if(!wpIdList.contains(wp.getWholesalePriceId().toString())){
				deleteById(wp.getWholesalePriceId());
			}
		}
		//增加或者修改批发价
		for (int i = 0; i < wpIds.length; i++) {
			WholesalePrice wp;
			if(wpIds[i].equals("")){
				//case:新增
				wp = new WholesalePrice();
			}else{
				//case:修改
				wp = getById(Integer.valueOf(wpIds[i]));
			}
			if(!StringUtils.isEmpty(wpMinQuantitys[i])){
				wp.setMinQuantity(Integer.parseInt(wpMinQuantitys[i]));
			}else{
				wp.setMinQuantity(0);
			}
			if(!StringUtils.isEmpty(wpMaxQuantitys[i])){
				wp.setMaxQuantity(Integer.parseInt(wpMaxQuantitys[i]));
			}else{
			}
			wp.setPrice(new BigDecimal(wpPrices[i]));
			
			wp.setProductSkuId(new Integer(wpSkuIds[i]));
			wp.setSortOrder(i);
			save(wp);
			newWpIdList.add(wp.getWholesalePriceId().toString());
		}
		return newWpIdList;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.WholesalePriceManager#doBulkEditWholesalePriceFromArrays(com.probiz.estore.common.model.product.ProductSku, java.lang.String[], java.lang.String[], java.lang.String[])
	 */
	public List<String> doBulkEditWholesalePriceFromArrays(ProductSku sku,String[] wpMinQuantitys, String[] wpMaxQuantitys,	String[] wpPrices){
		List<String> newWpIdList = new ArrayList<String>();
		//如果有批发价，则先把批发价清空
		for(WholesalePrice wp: sku.getWholesalePrices()){
			delete(wp);
		}
		//增加或者修改批发价
		for (int i = 0; i < wpPrices.length; i++) {
			WholesalePrice wp = new WholesalePrice();
			
			if(!StringUtils.isEmpty(wpMinQuantitys[i])){
				wp.setMinQuantity(Integer.parseInt(wpMinQuantitys[i]));
			}else{
				wp.setMinQuantity(0);
			}
			if(!StringUtils.isEmpty(wpMaxQuantitys[i])){
				wp.setMaxQuantity(Integer.parseInt(wpMaxQuantitys[i]));
			}else{
			}
			wp.setPrice(new BigDecimal(wpPrices[i]));
			
			wp.setProductSku(sku);
			wp.setSortOrder(i);
			save(wp);
			newWpIdList.add(wp.getWholesalePriceId().toString());
		}
		return newWpIdList;
		
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.WholesalePriceManager#saveWholesalePriceForImport(com.probiz.estore.common.model.product.ProductSku, java.util.List)
	 */
	public void saveWholesalePriceForImport(ProductSku productSku, List<WholesalePrice> wholesalePriceList){
		int i=0;
		List<WholesalePrice> oldWholesalePriceList = new ArrayList<WholesalePrice>();
		oldWholesalePriceList.addAll(productSku.getWholesalePrices());
		for(WholesalePrice wp: wholesalePriceList){
			if(i >=oldWholesalePriceList.size()){
				//有新增批发价条目
				wp.setProductSku(productSku);
				save(wp);
			}else{
				WholesalePrice existWholesalePrice = oldWholesalePriceList.get(i);
				existWholesalePrice.setMinQuantity(wp.getMinQuantity());
				existWholesalePrice.setMaxQuantity(wp.getMaxQuantity());
				existWholesalePrice.setPrice(wp.getPrice());
				existWholesalePrice.setSortOrder(wp.getSortOrder());
				save(existWholesalePrice);
			}
			i++;
		}
		if(i<=oldWholesalePriceList.size()-1){
			//删除多余的批发价
			for(int j=i;j<oldWholesalePriceList.size();j++){
				WholesalePrice deleteWholesalePrice = oldWholesalePriceList.get(j);
				delete(deleteWholesalePrice);
			}
		}
	}
}
